﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Data.SqlClient;
using ServiceDeskUNAB.Controlador;
using ServiceDeskUNAB.Modelo;

namespace ServiceDeskUNAB.Vista
{
    public partial class NuevoSOVista : UserControl
    {
        public NuevoSOVista()
        {
            InitializeComponent();
        }

        private void agregarBtn_Click(object sender, EventArgs e)
        {
            if (Conexion.Instance.Ingresar("SO", nuevaCatTxt.Text))
            {
                MessageBox.Show("Ingreso Exitoso");

                using (Conexion.Instance.Con) //With this, you don't have to close the connection
                {

                    SqlDataAdapter adapter = new SqlDataAdapter("select * from SO", Conexion.Instance.Con);
                    DataSet ds = new DataSet();
                    adapter.Fill(ds, "SO");
                    dataGridViewRoles.DataSource = ds;
                    dataGridViewRoles.DataMember = "SO";
                }

            }
            else
                MessageBox.Show("Error de Ingreso");
        }

        private void NuevaCategoriaIncidenciasVista_Load(object sender, EventArgs e)
        {
            using (Conexion.Instance.Con) //With this, you don't have to close the connection
            {

                SqlDataAdapter adapter = new SqlDataAdapter("select * from SO", Conexion.Instance.Con);
                DataSet ds = new DataSet();
                adapter.Fill(ds, "SO");
                dataGridViewRoles.DataSource = ds;
                dataGridViewRoles.DataMember = "SO";
            }
        }

       

        private void dataGridViewCategorias_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridViewRoles.SelectedCells.Count > 0)
            {
                int selectedrowindex = dataGridViewRoles.SelectedCells[0].RowIndex;

                DataGridViewRow selectedRow = dataGridViewRoles.Rows[selectedrowindex];

                string id = Convert.ToString(selectedRow.Cells[this.dataGridViewRoles.Columns[0].Name].Value);
                string nombre = Convert.ToString(selectedRow.Cells[this.dataGridViewRoles.Columns[1].Name].Value);
                idBox.Text = id;
                nombreTxt.Text = nombre;
            }
        }

        private void buscarBtn_Click(object sender, EventArgs e)
        {
            CampoIncidencia ci = Conexion.Instance.Buscar("SO",int.Parse(idBox.Text));
            if (ci == null)
                MessageBox.Show("No se encuentra SO");
            else
                nombreTxt.Text = ci.Nombre;

        }

        private void eliminarBtn_Click(object sender, EventArgs e)
        {

        }

        private void modificarBtn_Click(object sender, EventArgs e)
        {
            if (Conexion.Instance.Ingresar("SO", nuevaCatTxt.Text))
                MessageBox.Show("Ingreso Exitoso");
            else
                MessageBox.Show("Error de Ingreso");
        }
    }
}
